package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.module.common.utility.Constant;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.build.BuildMain;
import com.motu.monstercity.server.game.commondata.partner.PartnerConstant;
import com.motu.monstercity.server.game.commondata.restaurant.RestaurantFood;
import com.motu.monstercity.server.game.commondata.restaurant.RestaurantFoodModel;
import com.motu.monstercity.server.game.commondata.restaurant.RestaurantGuest;
import com.motu.monstercity.server.game.commondata.restaurant.RestaurantUpgrade;
import com.motu.monstercity.server.game.commondata.skill.Skill;
import com.motu.monstercity.server.game.commondata.skill.SkillConstant;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;

import java.util.List;

public class RestaurantManager extends RestaurantUserData {
    // 解锁默认数据
    public static void unlockDefaultData(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower, UserRestaurantInfo userRestaurantInfo) {
        for (int i=0;i< AllParam.RESTAURANT_DEFAULT_MAIN.length;i++) {
            int restaurantId = AllParam.RESTAURANT_DEFAULT_MAIN[i];
            unlockRestaurantRoom(pbUserData, userInfo, userPower, userRestaurantInfo, restaurantId);
        }

        for (int i=0;i< AllParam.RESTAURANT_DEFAULT_GUEST.length;i++) {
            int guestId = AllParam.RESTAURANT_DEFAULT_GUEST[i];
            unlockRestaurantGuest(userInfo.getId(), userRestaurantInfo, guestId);
        }
        userRestaurantInfo.update();
    }

    // 解锁餐厅，
    public static void unlockRestaurantRoom(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower, UserRestaurantInfo userRestaurantInfo, int restaurantId) {
        long userId = userInfo.getId();
        JsonArray jsonArray = userRestaurantInfo.getUnlockRoomArray();
        if (Tool.isInList(jsonArray, restaurantId)) {
            return;// 已经解锁过了
        }
        jsonArray.add(restaurantId);
        userRestaurantInfo.putUnlockRoom(jsonArray.toString());

        UserRestaurantRoom userRestaurantRoom = getUserRestaurantRoom(userId, restaurantId);
        if (userRestaurantRoom == null) {
            int level = 1;// 初始默认1级
            userRestaurantRoom = new UserRestaurantRoom();
            userRestaurantRoom.putUserId(userId);
            userRestaurantRoom.putRestaurantId(restaurantId);
            userRestaurantRoom.putLevel(pbUserData, userInfo, userPower, userRestaurantInfo, level, false);// 不更新userRestaurantInfo，外面统一更新
            userRestaurantRoom.doCreate();
            ProtoDataUtils.updatePBUserData(pbUserData, userRestaurantRoom, true);
        }
    }

    // 解锁美食攻略 isUpdateRestaurantInfo 是否更新中餐厅的用户信息，false不更新，外层统一更新，解锁餐厅传false,升级餐厅传true
    public static void unlockRestaurantFood(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower, UserRestaurantInfo userRestaurantInfo, int foodId, boolean isUpdateRestaurantInfo) {
        long userId = userInfo.getId();
        RestaurantFood restaurantFood = getRestaurantFood(foodId);
        if (restaurantFood == null) {
            return;
        }
        JsonArray jsonArray = userRestaurantInfo.getUnlockFoodArray();
        if (Tool.isInList(jsonArray, foodId)) {
            return;// 已经解锁过了
        }
        jsonArray.add(foodId);
        userRestaurantInfo.putUnlockFood(jsonArray.toString());
        if (isUpdateRestaurantInfo) {// 是否更新中餐厅的用户信息，false不更新，外层统一更新
            userRestaurantInfo.update();
            ProtoDataUtils.updatePBUserData(pbUserData, userRestaurantInfo);
        }

        UserRestaurantFood userRestaurantFood = getUserRestaurantFood(userId, foodId);
        if (userRestaurantFood == null) {
            int level = 1;// 初始默认1级
            userRestaurantFood = new UserRestaurantFood();
            userRestaurantFood.putUserId(userId);
            userRestaurantFood.putFoodId(foodId);
            userRestaurantFood.putLevel(level);
            userRestaurantFood.doCreate();
            ProtoDataUtils.updatePBUserData(pbUserData, userRestaurantFood, true);

            int skillId = restaurantFood.getSkillId();// 技能id，配置在skill表
            Skill skill = SkillManager.getSkill(skillId);
            handleFoodLeveUp(pbUserData, userInfo, userPower, skill, level);// 美食攻略对应的技能升级，对相应性格的商业建筑有加成
        }
    }

    // 解锁客人
    public static void unlockRestaurantGuest(long userId, UserRestaurantInfo userRestaurantInfo, int guestId) {
        RestaurantGuest restaurantGuest = getRestaurantGuest(guestId);
        if (restaurantGuest == null) {
            return;
        }
        JsonArray jsonArray = userRestaurantInfo.getUnlockGuestArray();
        if (Tool.isInList(jsonArray, guestId)) {
            return;// 已经解锁过了
        }
        jsonArray.add(guestId);
        userRestaurantInfo.putUnlockGuest(jsonArray.toString());
    }

    // 根据权重，从已解锁的客人中随机获取一个，招待成功计算熟练度加成用
    public static RestaurantGuest getRandomGuest(UserRestaurantInfo userRestaurantInfo) {
        JsonArray jsonArray = userRestaurantInfo.getUnlockGuestArray();
        int max = jsonArray.size();
        int[] rates = new int[max];
        for (int i=0; i< max; i++)  {
            int guestId = jsonArray.getInteger(i);
            RestaurantGuest restaurantGuest = getRestaurantGuest(guestId);
            rates[i] = restaurantGuest.getWeight();
        }
        int random = Tool.getWeightIndex(rates);// 随机结果
        return getRestaurantGuest(jsonArray.getInteger(random));
    }

    // 根据权重，从已解锁的美食攻略里随机获取一个
    public static RestaurantFood getRandomFood(UserRestaurantInfo userRestaurantInfo) {
        JsonArray jsonArray = userRestaurantInfo.getUnlockFoodArray();
        int max = jsonArray.size();
        int[] rates = new int[max];
        for (int i=0; i< max; i++)  {
            int foodId = jsonArray.getInteger(i);
            RestaurantFood restaurantFood = getRestaurantFood(foodId);
            rates[i] = restaurantFood.getWeight();
        }
        int random = Tool.getWeightIndex(rates);// 随机结果
        return getRestaurantFood(jsonArray.getInteger(random));
    }

    // 判断这个美食攻略是否完成，需要的餐厅全部解锁表示完成
    public static boolean checkRestaurantFoodComplete(UserRestaurantInfo userRestaurantInfo, RestaurantFood restaurantFood) {
        JsonArray jsonArray = userRestaurantInfo.getUnlockRoomArray();
        JsonArray needArray = restaurantFood.takeNeedRoomArray();// 需要解锁的餐厅列表
        for (int i=0; i<needArray.size(); i++) {
            int aa = needArray.getInteger(i);
            if (!Tool.isInList(jsonArray, aa)) {
                return false;// 只要有一个餐厅未解锁，则表示接待失败
            }
        }
        return true;
    }

    /**
     * 完成美食攻略，获得的熟练度总数/人气值总数，遍历所有需要的餐厅
     * @param restaurantFood
     * @param roomList
     * @param valueType 0-总的熟练度，1-人气值
     * @return
     */
    public static int countFoodAddValue(RestaurantFood restaurantFood, List<UserRestaurantRoom> roomList, int valueType) {
        int total = 0;
        JsonArray needArray = restaurantFood.takeNeedRoomArray();// 需要解锁的餐厅列表
        for (UserRestaurantRoom userRestaurantRoom : roomList) {
            int restaurantId = userRestaurantRoom.getRestaurantId();
            if (Tool.isInList(needArray, restaurantId)) {
                RestaurantUpgrade restaurantUpgrade = getRestaurantUpgrade(restaurantId, userRestaurantRoom.getLevel());
                if (valueType == 1) {
                    total += restaurantUpgrade.getScoreValue();// 总的人气值
                } else {
                    total += restaurantUpgrade.getSkillValue();// 总的熟练度
                }
            }
        }
        return total;
    }

    // 美食攻略对应的技能升级，对相应性格的商业建筑有加成
    public static void handleFoodLeveUp(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower, Skill skill, int newLevel) {
        if (skill == null || skill.getEffectType() != SkillConstant.EFFECT_TYPE_55) {
            return;
        }
        // 增量更新  技能加成值数组[100,200,300,400,500,600]
        int charType = skill.getEffectParam();// 美食攻略对应的技能对哪个性格的商业建筑有加成，配置在skill
        int addValue = skill.takeLevelUpDiff(newLevel);// 增量
        JsonArray jsonArray = SkillManager.addSkillEffectArray(charType, addValue, userPower.getEarnAddFoodPerArray());
        userPower.putEarnAddFoodPer(jsonArray.toString());
        List<UserBuild> buildList = userPower.takeUserBuildList(charType);// 对应的商业建筑
        BuildManager.batchUpdateBuildEarn(pbUserData, userInfo, userPower, buildList);// 批量更新商业建筑的城市收益，并更新userpower下发给客户端
    }

    /**
     * 美食攻略对建筑的收益的加成
     * @return jsonarray格式的字符串，固定6个值CHARACTER_TYPE_SIZE + 1，第一个表示所有性格，第二个表示性格1
     */
    public static String getEarnAddFood(List<UserRestaurantFood> foodList) {
        JsonArray jsonArray = SkillManager.createSkillEffectArray();
        int value0 = 0;
        int value1 = 0;
        int value2 = 0;
        int value3 = 0;
        int value4 = 0;
        int value5 = 0;
        for (UserRestaurantFood userRestaurantFood : foodList) {
            RestaurantFood restaurantFood = getRestaurantFood(userRestaurantFood.getFoodId());
            if (restaurantFood == null || restaurantFood.getSkillId() == 0) {
                continue;
            }
            Skill skill = SkillManager.getSkill(restaurantFood.getSkillId());
            if (skill.getEffectType() != SkillConstant.EFFECT_TYPE_55) {
                continue;
            }
            int addValue = skill.countTotalAdd(userRestaurantFood.getLevel());

            if (skill.getEffectParam() == 0) {// 该技能加成在哪个性格的干员。配置在effectparam
                value0 += addValue;
            } else if (skill.getEffectParam() == Constant.CHARACTER_TYPE_1) {
                value1 += addValue;
            } else if (skill.getEffectParam() == Constant.CHARACTER_TYPE_2) {
                value2 += addValue;
            } else if (skill.getEffectParam() == Constant.CHARACTER_TYPE_3) {
                value3 += addValue;
            } else if (skill.getEffectParam() == Constant.CHARACTER_TYPE_4) {
                value4 += addValue;
            } else if (skill.getEffectParam() == Constant.CHARACTER_TYPE_5) {
                value5 += addValue;
            }
        }
        jsonArray.set(0, value0);
        jsonArray.set(Constant.CHARACTER_TYPE_1, value1);
        jsonArray.set(Constant.CHARACTER_TYPE_2, value2);
        jsonArray.set(Constant.CHARACTER_TYPE_3, value3);
        jsonArray.set(Constant.CHARACTER_TYPE_4, value4);
        jsonArray.set(Constant.CHARACTER_TYPE_5, value5);

        return jsonArray.toString();
    }

}
